package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.interceptor.JwtTokenUserInterceptor;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.ShoppingCarService;
import com.sky.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/shoppingCart")
public class ShoppingCarController {

    @Autowired
    private ShoppingCarService shoppingCarService;

    @Autowired
    JwtProperties jwtProperties;

    /**
     * 添加购物车
     * @param httpServletRequest
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    public Result addShoppingCar(HttpServletRequest httpServletRequest, @RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("addShoppingCar的值是:{}",shoppingCartDTO);
        log.info("HttpServletRequest的值是:{}",httpServletRequest);

        String token = httpServletRequest.getHeader(jwtProperties.getUserTokenName());
        Claims JWT = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(),token);

        Long userId = JWT.get("userId", Long.class); // 注意：userId 是 Long 类型

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        shoppingCart.setUserId(userId);
        shoppingCarService.insert(shoppingCart);
        return Result.success();
    };

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("list")
    public Result<List<ShoppingCart>> getShoppingCartList(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader(jwtProperties.getUserTokenName());
        Claims JWT = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(),token);
        Long userId = JWT.get("userId", Long.class);
        return Result.success(shoppingCarService.list(userId));
    };

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public Result cleanShoppingCart(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader(jwtProperties.getUserTokenName());
        Claims JWT = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(),token);
        Long userId = JWT.get("userId", Long.class);
        shoppingCarService.cleanShoppingCart(userId);
        return Result.success();
    }

    /**
     * 删除一个购物车商品
     * @return
     */
    @PostMapping("/sub")
    public Result subShoppingCart(HttpServletRequest httpServletRequest ,@RequestBody ShoppingCartDTO shoppingCartDTO){
        String token = httpServletRequest.getHeader(jwtProperties.getUserTokenName());
        Claims JWT = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(),token);
        Long userId = JWT.get("userId", Long.class);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(userId);
        shoppingCarService.subShoppingCart(shoppingCart);
        return Result.success();
    }

}
